#include "framescene.h"
#include <iostream>

CFrameBufferObject::CFrameBufferObject( int width, int height )
{

	glGenTextures( 1, &m_fboTexture );

	glBindTexture( GL_TEXTURE_2D, m_fboTexture );

	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
	
	glGenFramebuffersEXT( 1, &m_fboObject );
	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_fboObject );
	glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_fboTexture, 0 );
	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );

}

CFrameBufferObject::~CFrameBufferObject()
{
	
	glDeleteFramebuffersEXT( 1, &m_fboObject );
	glDeleteTextures( 1, &m_fboTexture );

}

CFrameScene::CFrameScene()
{

	for( int j = 0; j < MAX_RENDER_LAYERS; j++ )
	{

		m_pFrameBuffers.push_back( new CFrameBufferObject( SCREEN_WIDTH, SCREEN_HEIGHT ) );
		
	}
	
}

CFrameScene::~CFrameScene()
{

	m_pFrameBuffers.clear();
	
}

void CFrameScene::Draw()
{

	PUSH_MATRIX;

	Draw::Scale( 1.0f, -1.0f, 1.0f );
	
	for( int j = 0; j < MAX_RENDER_LAYERS; j++ )
	{
	
		m_pFrameBuffers[j]->BindTexture();
		Draw::TexturedQuad( 0, -SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT );
		
	}
	
	POP_MATRIX;
	
}